package com.bdqn;

import com.bdqn.comm.SysUserVo;
import com.bdqn.dao.SysUserRepository;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class SysUserRepositoryTester {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysUserRepository sysUserRepository;
    @Test
    public void testInsert(){
        SysUser sysUser = new SysUser();
        sysUser.setUsrName("JPA");
        sysUser.setUsrPassword("123456");
        sysUser.setUsrFlag(2L);
        //sysUser.setSysRole(2L);
        SysUser save = sysUserRepository.save(sysUser);
        System.out.println(save.toString());

    }
    @Test
    public void testGet(){
        //User user = userRepository.getOne(2L);//不是使用该方法
        SysUser user = sysUserRepository.findById(2L).get();
        System.out.println("usrName："+user.getUsrName());
    }

    @Test
    public void testFindByUsrNameLike(){
        List<SysUser> list = sysUserRepository.findByUsrNameLike("%s%");//查询参数必须带%号
        if (list != null){
            for (SysUser sysUser : list) {
                System.out.println("usrName:" + sysUser.getUsrName());
            }
        }
    }

    @Test
    public void testFindByRoleId(){

    }
    @Test
    public void testFindPageByUsrRoleId(){  //测试按角色查询用户并分页
        int page=0,size=2;//分页要素，页数从0开始
        //控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC,"usrId");
        //控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<SysUser> userPage = sysUserRepository.findPageByUsrRoleId(4L,pageable);
        System.out.println("总记录数："+userPage.getTotalElements());
        System.out.println("总页数："+userPage.getTotalPages());
        System.out.println("当前页数："+(userPage.getNumber()+1));
        System.out.println("每页记录数："+userPage.getSize());
        System.out.println("当前页记录数："+userPage.getNumberOfElements());
        for (SysUser sysUser : userPage.getContent()) { //获得查询记录
            System.out.println("usrId：" + sysUser.getUsrId());
        }
    }

    @Test
    public void testFindPageByMap(){
        int page=0,size=2;//分页要素
        //控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "usrId");
        //控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Map param = new HashMap();
        param.put("roleId",8L); //测试一个查询条件，也可不给或给多个
        Page<SysUser> userPager = sysUserService.findPageByMap(param,pageable);
        System.out.println("总记录数 --------> " + userPager.getTotalElements());
        System.out.println("总页数 ----------> " + userPager.getTotalPages());
        System.out.println("当前页数 --------> " + userPager.getNumber());
        System.out.println("每页记录数 ------> " + userPager.getSize());
        System.out.println("当前记录： ------> ");
         for(SysUser user:userPager.getContent()){//获得查询记录
            System.out.println("usrId："+user.getUsrId());
         }
    }

    @Test
    public void testGetSysUserInfo(){
        SysUserVo sysUserInfo = sysUserRepository.getSysUserInfo(2L);
        System.out.println("usrName:" + sysUserInfo.getUsrName());
        System.out.println("roleName:" + sysUserInfo.getRoleName());
    }
}
